package com.hfuu.scheuding;

import com.hfuu.dao.read.custom.TbActivityReadDao;
import com.hfuu.dao.read.custom.TbActivityUserReadDao;
import com.hfuu.dao.read.custom.TbUserReadDao;
import com.hfuu.dao.write.custom.TbActivityWriteDao;
import com.hfuu.dao.write.custom.TbUserWriteDao;
import com.hfuu.model.po.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Author: admin
 * Date: 2018/5/2
 * time: 下午4:56
 * 定时任务处理
 */
@Component
public class VolunteerScheuding {

//1星 志愿服务时间累计100小时
//2星 志愿服务时间累计300小时
//3星 志愿服务时间累计600小时
//4星 志愿服务时间累计1000小时
//5星 志愿服务时间累计1500小时

    private Logger logger = LoggerFactory.getLogger(VolunteerScheuding.class);

    @Autowired
    private TbActivityReadDao tbActivityReadDao;

    @Autowired
    private TbActivityWriteDao tbActivityWriteDao;

    @Autowired
    private TbUserWriteDao tbUserWriteDao;

    @Autowired
    private TbUserReadDao tbUserReadDao;

    @Autowired
    private TbActivityUserReadDao tbActivityUserReadDao;


    /**
     * 定时处理活动状态更新，和更新用户志愿星级
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void sloveActivityStatusAndGrade() {

        logger.info("=======开始定时处理活动状态更新，和更新用户志愿星级======");
        //获得所有活动
        TbActivityExample example = new TbActivityExample();
        TbActivityExample.Criteria criteria = example.createCriteria();
        criteria.andDisabledEqualTo(0);
        List<TbActivity> tbActivities = tbActivityReadDao.selectByExample(example);
        if (CollectionUtils.isEmpty(tbActivities)) {
            return ;
        }
        for (TbActivity tbActivity : tbActivities) {
            if (tbActivity.getStatus() == 3) {
                continue;
            }
            Date startTime = tbActivity.getStartTime();
            Date endTime = tbActivity.getEndTime();
            Date now = new Date();
            Integer status = 0;

            if (now.compareTo(startTime) < 0) {
                status = 1;
            } else if (now.compareTo(startTime) >= 0 && now.compareTo(endTime) <= 0) {
                status = 2;
            } else {
                status = 3;
            }
            //更新活动状态
            tbActivity.setStatus(status);
            tbActivityWriteDao.updateByPrimaryKeySelective(tbActivity);
            //更新用户星级
            if (status == 3) {
                //获得星级后，判断是否需要更新升级
                TbActivityUserExample example1 = new TbActivityUserExample();
                TbActivityUserExample.Criteria criteria1 = example1.createCriteria();
                criteria1.andDisabledEqualTo(0).andActivityIdEqualTo(tbActivity.getId());
                List<TbActivityUser> tbActivityUsers = tbActivityUserReadDao.selectByExample(example1);
                if (CollectionUtils.isEmpty(tbActivityUsers)) {
                    continue;
                }

                Integer differ = getTimeDiffer(tbActivity.getStartTime(), tbActivity.getEndTime());
                if (differ <= 0) {
                    continue;
                }
                for (TbActivityUser obj : tbActivityUsers) {
                    TbUser tbUser = tbUserReadDao.selectByPrimaryKey(obj.getUserId());
                    Integer volunteerTime = tbUser.getVolunteerTime();
                    volunteerTime += differ;
                    tbUser.setVolunteerTime(volunteerTime);
                    if (volunteerTime < 100 && volunteerTime > 0) {
                        tbUser.setVolunteerGrade(0);
                    } else if (volunteerTime < 300 && volunteerTime >= 100) {
                        tbUser.setVolunteerGrade(1);
                    } else if (volunteerTime < 600 && volunteerTime >= 300) {
                        tbUser.setVolunteerGrade(2);
                    } else if (volunteerTime < 1000 && volunteerTime >= 600) {
                        tbUser.setVolunteerGrade(3);
                    } else if (volunteerTime < 1500 && volunteerTime >= 1000) {
                        tbUser.setVolunteerGrade(4);
                    } else {
                        tbUser.setVolunteerGrade(5);
                    }
                    tbUserWriteDao.updateByPrimaryKeySelective(tbUser);
                }
            }

        }
        logger.info("=======结束定时处理活动状态更新，和更新用户志愿星级======");

    }


    /**
     * 活动两个小时小时差
     * @param start
     * @param end
     * @return
     */
    private Integer getTimeDiffer(Date start, Date end) {

        if (start == null || end == null) {
            return 0;
        }

        return  (int) ((end.getTime() - start.getTime())/(1000 * 60 * 60));
    }
}
